package com.coocaa.internship.service.serviceImpl;

import com.coocaa.internship.common.ResultCode;
import com.coocaa.internship.common.ResultData;
import com.coocaa.internship.config.FileServerConfig;
import com.coocaa.internship.dao.UserCvDao;
import com.coocaa.internship.exception.BaseException;
import com.coocaa.internship.pojo.UserCV;
import com.coocaa.internship.service.UserCvService;
import com.coocaa.internship.util.FileNameUtil;
import com.coocaa.internship.util.FileSuffixUtil;
import com.coocaa.internship.util.FileUploadUtil;
import com.coocaa.internship.util.ParamvalidatorUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @Description: 用户简历 service实现
 * @Author: zwh
 * @Date: 2019-07-28
 * @Time: 21:24
 */
@Slf4j
@Service
public class UserCvServiceImpl implements UserCvService {

    @Autowired
    UserCvDao userCvDao;

    @Autowired
    UserLoginServiceImpl userLoginService;

    @Autowired
    FileServerConfig fileUploadConfig;

    @Override
    public ResultData insertUserCvByUserId(Long userId, MultipartFile file) {
        //检查是否存在该用户
        if (userLoginService.checkUserById(userId)) {
            //判断文件是否为空
            if (file.isEmpty()) {
                throw new BaseException(ResultCode.FILE_NOT_EXISTS.getCode()
                        , ResultCode.FILE_NOT_EXISTS.getMessage());
            }
            //文件原始名字
            String originFileName = file.getOriginalFilename();
            //文件后缀
            assert originFileName != null;
            String suffix = originFileName.substring(originFileName.lastIndexOf(".") + 1, originFileName.length());
            if (FileSuffixUtil.checkFileSuffix(suffix)) {
                //文件真实存储名字
                String newFileName = FileNameUtil.getNewName(originFileName);
                //文件存放路径
                String uploadPath = fileUploadConfig.getUploadPath() + "/" + userId + "/" + newFileName;
                //文件存储
                boolean result = FileUploadUtil.upload(file, uploadPath);
                if (result) {
                    //将简历的访问地址存入数据库
                    String downloadPath = fileUploadConfig.getDownloadPath() + "/" + userId + uploadPath.substring(uploadPath.lastIndexOf("/"));
                    //封装简历信息
                    UserCV userCV = UserCV.builder()
                            .userId(userId)
                            .userCvStatus(0)
                            .userCvPdf(downloadPath).build();
                    Integer integer = userCvDao.insertUserCv(userCV);
                    if (integer > 0) {
                        return ResultData.success(null);
                    }
                }
            } else {
                throw new BaseException(ResultCode.FILE_SUFFIX_ILLEGAL.getCode()
                        , ResultCode.FILE_SUFFIX_ILLEGAL.getMessage());
            }
        }
        return ResultData.builder()
                .code(ResultCode.USERLOGIN_NOT_EXISTS.getCode())
                .message(ResultCode.USERLOGIN_NOT_EXISTS.getMessage()).build();
    }

    @Override
    public ResultData selectUserCvByUserId(Long userId) {
        if (!userLoginService.checkUserById(userId)) {
            throw new BaseException(ResultCode.USERLOGIN_NOT_EXISTS.getCode()
                    , ResultCode.USERLOGIN_NOT_EXISTS.getMessage());
        }
        List<UserCV> userCVs = userCvDao.selectUserCvByUserId(userId);
        if (userCVs.isEmpty()) {
            return ResultData.success("没有任何简历");
        }
        return ResultData.success(userCVs);
    }

    @Override
    public ResultData selectUserCvById(Long userCvId) {
        UserCV userCV = userCvDao.selectUserCvById(userCvId);
        if (userCV != null) {
            return ResultData.success(userCV);
        }
        return ResultData.failed();
    }

    @Override
    public ResultData selectDeliveredCvByUserId(Long userId) {
        if (!userLoginService.checkUserById(userId)) {
            throw new BaseException(ResultCode.USERLOGIN_NOT_EXISTS.getCode()
                    , ResultCode.USERLOGIN_NOT_EXISTS.getMessage());
        }
        List<UserCV> userCVs = userCvDao.selectDeliveredCvByUserId(userId);
        if (userCVs.isEmpty()) {
            return ResultData.success("还没有投递过简历");
        }
        return ResultData.success(userCVs);
    }

    @Override
    public ResultData deleteUserCvById(Long userCvId) {
        ParamvalidatorUtil.paramValidate(userCvId);
        Integer integer = userCvDao.deleteUserCvByUserCvId(userCvId);
        if (integer > 0) {
            return ResultData.success(null);
        }
        return ResultData.failed();
    }

    @Override
    public Boolean changStatusToDelivered(Long userCvId) {
        ParamvalidatorUtil.paramValidate(userCvId);
        Integer integer = userCvDao.increaseUserCvStatusByCvId(userCvId);
        if (integer > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean changStatusToUnDeliver(Long userCvId) {
        ParamvalidatorUtil.paramValidate(userCvId);
        Integer integer = userCvDao.decreaseUserCvStatusByCvId(userCvId);
        if (integer > 0) {
            return true;
        }
        return false;
    }
}
